package com.ah.hf.controller;

import com.ah.hf.common.R;
import com.ah.hf.dto.SetmealDto;
import com.ah.hf.entity.Category;
import com.ah.hf.entity.Setmeal;
import com.ah.hf.service.CategoryService;
import com.ah.hf.service.SetmealDishService;
import com.ah.hf.service.SetmealService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import sun.util.resources.ga.LocaleNames_ga;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@Slf4j
//套餐管理
@RequestMapping("/setmeal")
public class SeteamlController {

    @Autowired
    private SetmealService setmealService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private SetmealDishService setmealDishService;

    //新增套餐
    @PostMapping
     public R<String> save(@RequestBody SetmealDto setmealDto){
        log.info("接收到的数据 : {}",setmealDto);
        setmealService.saveWithDish(setmealDto);
        return R.success("添加成功");
     }


     @GetMapping("/page")
     public R<Page> page(int page , int pageSize, String name){
        //构造分页Setmeal对象
         Page<Setmeal> setmealPage = new Page<>(page,pageSize);
         //构造分页SetmealDto对象
         Page<SetmealDto> setmealDtoPage = new Page<>();

         //条件构造器
         LambdaQueryWrapper<Setmeal> qw = new LambdaQueryWrapper<>();
         //模糊查询
         qw.like(StringUtils.isNotEmpty(name),Setmeal::getName,name);
         //排序条件
         qw.orderByDesc(Setmeal::getUpdateTime);
         //分页查询
         setmealService.page(setmealPage, qw);

         //拷贝对象，忽略records
         BeanUtils.copyProperties(setmealPage,setmealDtoPage,"records");

         //获取records属性
         List<Setmeal> records = setmealPage.getRecords();

         //遍历赋值，查询数据
         List<SetmealDto> list = records.stream().map((item)->{
             //构造 SetmealDto 对象
             SetmealDto setmealDto = new SetmealDto();
             //拷贝对象
             BeanUtils.copyProperties(item,setmealDto);
             //获取id
             Long categoryId = item.getCategoryId();
             //根据id查询 Category 对象
             Category category = categoryService.getById(categoryId);
             //赋值给 SetmealDto 对象
             if (category!=null){
                 setmealDto.setCategoryName(category.getName());
             }
             return setmealDto;
         }).collect(Collectors.toList());

         //设置属性(Records 此list集合包含实体类中所有属性)
         setmealDtoPage.setRecords(list);

         //返回数据
         return R.success(setmealDtoPage);
     }

     //返回setmeal对象
     @GetMapping("/{id}")
     public R<Setmeal> select(@PathVariable int id){
         Setmeal setmeal = setmealService.getById(id);
         return R.success(setmeal);
     }

     @PostMapping("/status/{status}")
     public R<String> status(@PathVariable int status,@RequestParam("ids") List<Long> ids){
        //根据id改变状态
         UpdateWrapper<Setmeal> qw = new UpdateWrapper<>();
         //where (id in (? , ? , ?))
         qw.in("id",ids);
         qw.set("status",status);
         setmealService.update(qw);
         return R.success("成功");
     }

    @DeleteMapping
    //删除套餐按钮
    public R<String> delete(@RequestParam List<Long> ids){
        log.info("ids : {}",ids);
        setmealService.deleteWithDish(ids);
        return R.success("套餐数据删除成功");
     }

     /*@PostMapping("/{status}")
     public R<String> status(@PathVariable int status,@RequestParam List<Long> ids){

        return null;
     }*/

    //获取套餐信息
    @GetMapping("/list")
    public R<List<Setmeal>> listSetmeal(Setmeal setmeal){
        LambdaQueryWrapper<Setmeal> qw = new LambdaQueryWrapper<>();
        //根据条件查询
        qw.eq(setmeal.getCategoryId() != null,
                Setmeal::getCategoryId,setmeal.getCategoryId());
        qw.eq(setmeal.getStatus() != null,
                Setmeal::getStatus,setmeal.getStatus());
        qw.orderByDesc(Setmeal::getUpdateTime);
        List<Setmeal> list = setmealService.list(qw);
        return R.success(list);
    }
}
